<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="900px">
    <el-tabs v-model="subTabsName">
      <el-tab-pane label="文玩信息" name="infoMain">
        <el-form
          ref="formRef"
          :model="formData"
          :rules="formRules"
          v-loading="formLoading"
        >
          <el-row :gutter="10">
            <el-col :span="8">
              <el-form-item label="名字" prop="name">
                <el-input v-model="formData.name" placeholder="请输入名字" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="分类" prop="category">
                <el-select v-model="formData.category" placeholder="请选择分类">
                  <el-option
                    v-for="dict in getIntDictOptions(DICT_TYPE.ARTIFACT_CATEGORY)"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="材质" prop="material">
                <el-select v-model="formData.material" placeholder="请选择材质">
                  <el-option
                    v-for="dict in getIntDictOptions(DICT_TYPE.ARTIFACT_MATERIAL)"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="8">
              <el-form-item label="重量" prop="weight">
                <el-input v-model="formData.weight" placeholder="请输入重量" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="长度" prop="length">
                <el-input v-model="formData.length" placeholder="请输入长度" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="宽度" prop="width">
                <el-input v-model="formData.width" placeholder="请输入宽度" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="8">
              <el-form-item label="高度" prop="height">
                <el-input v-model="formData.height" placeholder="请输入高度" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="年代" prop="year">
                <el-input v-model="formData.year" placeholder="请输入年代" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="年代描述" prop="yearDescription">
                <el-input v-model="formData.yearDescription" placeholder="请输入年代描述" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="描述" prop="description">
            <Editor v-model="formData.description" height="300px" />
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="文玩图片" name="infoImage">
        <InfoImageForm ref="infoImageFormRef" :artifact-id="formData.id" />
      </el-tab-pane>
      <el-tab-pane label="文玩视频" name="infoVideo">
        <InfoVideoForm ref="infoVideoFormRef" :artifact-id="formData.id" />
      </el-tab-pane>
    </el-tabs>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>




<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { InfoApi, InfoVO } from '@/api/artifact/info'
import InfoImageForm from './components/InfoImageForm.vue'
import InfoVideoForm from './components/InfoVideoForm.vue'

/** 文玩信息 表单 */
defineOptions({ name: 'InfoEditForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: NaN,
  name: "",
  category: undefined,
  material: undefined,
  weight: undefined,
  length: undefined,
  width: undefined,
  height: undefined,
  year: undefined,
  yearDescription: "",
  description: "",
})
const formRules = reactive({
  name: [{ required: true, message: '名字不能为空', trigger: 'blur' }],
  category: [{ required: true, message: '分类不能为空', trigger: 'change' }],
  material: [{ required: true, message: '材质不能为空', trigger: 'change' }],
})
const formRef = ref() // 表单 Ref

/** 子表的表单 */
const subTabsName = ref('infoMain')
const infoImageFormRef = ref()
const infoVideoFormRef = ref()

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await InfoApi.getInfo(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 校验子表单
  try {
    await infoImageFormRef.value.validate()
  } catch (e) {
    subTabsName.value = 'infoImage'
    return
  }
  try {
    await infoVideoFormRef.value.validate()
  } catch (e) {
    subTabsName.value = 'infoVideo'
    return
  }
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as InfoVO
    // 拼接子表的数据
    data.infoImages = infoImageFormRef.value.getData()
    data.infoVideos = infoVideoFormRef.value.getData()
    if (formType.value === 'create') {
      await InfoApi.createInfo(data)
      message.success(t('common.createSuccess'))
    } else {
      await InfoApi.updateInfo(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: NaN,
    name: "",
    category: undefined,
    material: undefined,
    weight: undefined,
    length: undefined,
    width: undefined,
    height: undefined,
    year: undefined,
    yearDescription: "",
    description: "",
  }
  formRef.value?.resetFields()
}
</script>
